<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/4
 * Time: 11:11
 */

namespace common\business\employee;


use common\consts\CommonResources;
use Yii;
use common\business\DbHandler;
use common\models\Employee;
use common\result\ActionResult;

class EmployeeApiHandler extends DbHandler
{
    /**
     * @param $obj Employee
     * @return ActionResult
     */
    public function createEmployee($obj){
        return $this->create($obj);
    }

    /**
     * @param Employee $newObj
     * @param Employee $oldObj
     * @return ActionResult
     */
    public function updateEmployee($newObj, $oldObj = null){
        if (!$oldObj){
            $oldObj = Employee::findOne([
                'number' => $newObj->number,
            ]);
        }
        return $this->update($newObj, $oldObj, [
            'name',
            'number',
            'gender',
            'birthday',
            'email',
            'mobile',
            'id_card_num',
            'native_place',
            'blood_type',
            'wed_status',
            'nationality',
            'folk',
            'hklx',
            'hr_id',
            'weixin',
            'position_name',
            'position_number',
            'labor_relation_state',
            'department_number',
            'department_name',
            'department_display_name'
        ]);
    }

    /**
     * @param $obj Employee
     * @return ActionResult
     */
    public function deleteEmployee($obj){
        return $this->delete($obj);
    }


    /**
     * @param $idListArr array
     * @return ActionResult
     */
    public function batchDeleteEmployee($idListArr){
        $action = Yii::t('app' , 'Batch Delete');
        $modelClass = Yii::t('app' , 'Employee');
        $totalResult = new ActionResult();
        foreach ($idListArr as $id){
            $objInDb = Employee::findOne($id);
            if ($objInDb){
                $totalResult->addSubResult($this->delete($objInDb));
            }
            else {
                $totalResult->addSubResult(new ActionResult(false, CommonResources::ACTION_FAIL_MSG($action, $modelClass, 'Record Not Found With id: '.$id)));
            }
        }
        return $totalResult;
    }
}